//封装http请求
import { IHttpGetParams } from "../../typings";

//实现项目服务器向聚合服务器请求数据
const nodeFetch = require('node-fetch');

import {formatParams } from '../lib/utils';

module.exports = {
  httpGet(options: IHttpGetParams): Promise<void> {
    //success和fail均为回调函数
    const { url, data, success, fail } = options;
    const {  config } = this.app;

    //拿到egg配置项里的APP_KEY
    const app_key = config.APP_KEY;
    
    //将data对象里的属性格式化为 type=top&key=xxxx
    const path = formatParams(data, app_key);

    return (
      nodeFetch(url + path)
        //json化数据
        .then((res) => res.json())
        .then((data) => success(data.result.data))
        .catch((err) => fail(err))
    );
  }
};
